package com.vgemv.jsutilitysdk.Menu.JsMenuView;

//Adapter负责提供数据

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemViewDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;


class JsMenuViewDefaultAdapter extends JsMenuViewAdapter {

    public JsMenuViewAdapterCallback adapterCallback;

    /**
     * onCreateViewHolder,表示Adapter当前要创建ViewHolder了.只有在需要的时候才会创建
     * 创建ViewHolder
     * $0 parent  是ItemView将要添加到的父容器.
     * $1,viewType,是自己传入的一个参数,用来创建不同类型的ItemView.
     */
    @Override
    public JsMenuItemViewHolderDefault onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        JsMenuItemView cellView = null;
        if(adapterCallback != null) {
            cellView = adapterCallback.jsMenuViewAdpaterCreateItemView(parent.getContext());
        }
        if(cellView == null) {
            cellView = new JsMenuItemViewDefault(parent.getContext());
        }
        return new JsMenuItemViewHolderDefault(cellView);
    }

    /*
     Description:
     1,将要在指定位置显示viewholder,将指定位置的数据,赋值给viewHodlerd的控件.
     2,需要在此时设置view的lp
     3,需要绑定点击事件.
     TODO:
     4,如果元素增加或者删除,会重新调用吗?
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {

        if(adapterCallback != null) {
            adapterCallback.jsMenuItemViewWillDisplay(viewHolder,position);
        }
        JsMenuItemViewHolderDefault holder = (JsMenuItemViewHolderDefault)viewHolder;

        final JsMenuItemVm vm = (JsMenuItemVm)mal_vms.get(position);
        vm.index = position;
        holder.setVm(vm);

        //此时为view设置点击函数
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterCallback != null) {
                    adapterCallback.jsMenuViewAdapterClickItem(JsMenuViewDefaultAdapter.this, position, vm);
                }
            }
        });
    }



    @Override
    public int getItemCount() {
        return mal_vms.size();
    }

}